<?php

class link extends Admin {

    private $name = "友情链接";
    private $list = null;

    function __construct() {
        parent::__construct();
        $this->list = $this->model->Cache()->get( 'link' );
        $this->model->table = "link";
    }

    public function index( $c, $a ) {
        include $this->display( $c );
    }

    public function add( $c, $a ) {
        include $this->display( $c );
    }

    public function edit( $c, $a ) {
        if ( !$id = intval( R( 'id' ) ) ) {
            $this->error();
        }
        $result = $this->list[ $id ];
        include $this->display( $c );
    }

    public function save( $c, $a ) {
        foreach ( $_POST as $key => $val ) {
            $rows[ $key ] = htmlspecialchars( trim( $val ) );
        }
        if ( $id = intval( R( "id" ) ) ) {
            $this->model->update( array( 'id' => $id ), $rows );
        } else {
            $this->model->create( $rows );
        }

        // 更新缓存
        $this->model->Cache()->update( 'link' );
        $this->success( '保存友情链接成功!' );
    }

    // 删除链接
    public function del() {
        if ( !$id = intval( R( "id" ) ) ) {
            $this->error();
        }

        $this->model->delete( array( 'id' => $id ) );

        // 更新缓存
        $this->model->Cache()->update( 'link' );
        $this->success( '删除友情链接成功!' );
    }
}